#include <REGX52.H>
#include <stdio.h>
#include "Delay.h"
#include "LED88.h"

sbit BUZZ=P2^5;

unsigned char k=0,musici=0,num=0,cs=0,csms=0,n=0,hunum=0,hucs=0,n1=0;

unsigned int code Accent[]=
{
	62018,62221,62401,62582,62743,62903,63045,63185,63321,63441,63558,63670,
	63777,63872,63969,64054,64140,64216,64291,64360,64426,64489,64547,64607,
	64655,64704,64751,64795,64837,64876,64913,64948,64981,65012,65042,65070,
	65095,65120,65144,65166,65186,65206,65225,65242,65259,65274,65289,65303
};

unsigned int code Music[]=
{
	23,1,25,1,26,2,26,2,26,1,25,1,26,1,28,1,30,2,30,2,30,1,29,1,30,1,31,1,33,2,33,2,33,1,32,1,33,1,38,1,
	33,6,30,2,31,2,31,1,30,1,28,2,31,2,30,2,30,1,28,1,26,2,30,2,28,2,23,2,25,2,26,2,28,6,23,1,25,1,26,2,
	26,2,26,1,25,1,26,1,28,1,30,2,30,2,30,1,29,1,30,1,31,1,33,2,33,2,33,1,32,1,33,1,38,1,33,6,26,2,35,2,
	33,2,31,2,30,2,28,2,26,2,25,2,26,2,28,2,30,1,31,1,30,2,28,2,26,2,21,1,23,1,25,1,26,1,28,1,30,1,31,2,
	31,2,31,2,31,2,31,1,30,1,31,4,32,2,33,2,33,2,33,1,31,1,30,1,31,1,33,6,33,2,35,2,35,2,38,3,35,1,33,2,
	33,2,33,1,31,1,30,2,31,2,31,2,25,2,28,2,26,2,26,2,26,2
};

void code Timer0_Init(void)		//1毫秒@11.0592MHz
{
	TMOD = 0x01;			//设置定时器模式
	TL0 = 0x66;				//设置定时初始值
	TH0 = 0xFC;				//设置定时初始值
	TF0 = 0;				//清除TF0标志
	TR0 = 1;				//定时器0开始计时
	EA = 1;
	ET0 = 1;
}

void code Timer1_Init(void)		//10毫秒@11.0592MHz
{
	TMOD &= 0x10;			//设置定时器模式
	TL1 = 0x00;				//设置定时初始值
	TH1 = 0xDC;				//设置定时初始值
	TF1 = 0;				//清除TF1标志
	TR1 = 1;				//定时器1开始计时
	EA = 1;
	ET1 = 1;
}

unsigned char code Image[]=
{
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0xC2,0xC2,0x3C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0xC2,0xC2,0x3C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};

unsigned char Human0[]=
{
	0x03,0x0E,0x06,0x04
};

unsigned char Human1[]=
{
	0x02,0x0E,0x07,0x04
};

unsigned char HumanJump[]=
{
	0x06,0x1C,0x0C,0x08
};

unsigned char HumanJumpH[]=
{
	0x0C,0x38,0x18,0x10
};

unsigned char HumanHigh[]=
{
	0x08,0x38,0x18,0x14
};

unsigned char  HumanFall[]=
{
	0x04,0x1C,0x0C,0x0A
};

unsigned char  HumanDown[]=
{
	0x02,0x0E,0x06,0x05
};

void Timer0() interrupt 1
{
	TL0 = Accent[k]%256;				//设置定时初始值
	TH0 = Accent[k]/256;				//设置定时初始值
	BUZZ=!BUZZ;
}

void Environment () interrupt 3
{
	TL1 = 0x00;				//设置定时初始值
	TH1 = 0xDC;				//设置定时初始值
		for(num=0;num<=31;num++)
		{

				if(num==6 || num==24)
				{	
					for(n=0;n<7;n++)
					{
						for(hucs=0;hucs<4;hucs++)
						{
							LED_Show(hucs,HumanJump[hucs]);
						}
						for(cs=0;cs<8;cs++)
						{
							LED_Show(cs,Image[num+cs]);
						}
					}
					num++;
					for(n1=0;n1<2;n1++)
					{
						for(n=0;n<7;n++)
						{
							for(hucs=0;hucs<4;hucs++)
							{
								LED_Show(hucs,HumanJumpH[hucs]);
							}
							for(cs=0;cs<8;cs++)
							{
								LED_Show(cs,Image[num+cs]);
							}
						}
						num++;
					}
					for(n1=0;n1<4;n1++)
					{
						for(n=0;n<7;n++)
						{
							for(hucs=0;hucs<4;hucs++)
							{
								LED_Show(hucs,HumanHigh[hucs]);
							}
							for(cs=0;cs<8;cs++)
							{
								LED_Show(cs,Image[num+cs]);
							}
						}
						num++;
					}
					for(n1=0;n1<2;n1++)
					{
						for(n=0;n<7;n++)
						{
							for(hucs=0;hucs<4;hucs++)
							{
								LED_Show(hucs,HumanFall[hucs]);
							}
							for(cs=0;cs<8;cs++)
							{
								LED_Show(cs,Image[num+cs]);
							}
						}
						num++;
					}
					for(n=0;n<7;n++)
						{
							for(hucs=0;hucs<4;hucs++)
							{
								LED_Show(hucs,HumanDown[hucs]);
							}
							for(cs=0;cs<8;cs++)
							{
								LED_Show(cs,Image[num+cs]);
							}
						}
					num++;
				}
				else
				{
					for(n=0;n<7;n++)
					{
						for(hucs=0;hucs<4;hucs++)
						{
							LED_Show(hucs,Human0[hucs]);
						}
						for(cs=0;cs<8;cs++)
						{
							LED_Show(cs,Image[num+cs]);
						}
					}
				}
			for(n=0;n<7;n++)
			{
				for(hucs=0;hucs<4;hucs++)
				{
					LED_Show(hucs,Human1[hucs]);
				}
				for(cs=0;cs<8;cs++)
				{
					LED_Show(cs,Image[num+cs]);
				}
			}
		}
}

int main(void)
{		
	Timer0_Init();
	while(1)
	{
			k=Music[musici];
			musici++;
			Delay1ms(120*Music[musici]);
			musici++;
			TR0=0;
			Delay1ms(10);
			TR0=1;
			if(musici>=221)musici=0;
		while(P3_1==0)
		{
			Delay1ms(20);
			while(P3_1==0)
			{
				k=Music[musici];
			musici++;
			Delay1ms(120*Music[musici]);
			musici++;
			TR0=0;
			Delay1ms(10);
			TR0=1;
			if(musici>=221)musici=0;
			}
			Delay1ms(20);
			Timer1_Init();
			while(1)
			{
				if(P3_0==0)
				{
					Delay1ms(20);
					while(P3_0==0);
					Delay1ms(20);
					break;
				}
			}
		}
	}
}
